//
//  CustomProjectCountDownView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/21.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "CustomProjectCountDownView.h"

#import "BCTimerHandler.h"

#import "NSDate+BCustom.h"

@interface CustomProjectCountDownView () <BCTimerHandlerDelegate>

@property (nonatomic, strong) UIView *backgroundView;

/**
 *leftIconIV
 */
@property (nonatomic, strong) UIImageView *leftIconIV;
/**
 *leftLabel
 */
@property (nonatomic, strong) UILabel *leftLabel;

/**
 *rightIconIV
 */
@property (nonatomic, strong) UIImageView *rightIconIV;
/**
 *rightLabel
 */
@property (nonatomic, strong) UILabel *rightLabel;


/**
 *BCTimerHandler
 */
@property (nonatomic, strong) BCTimerHandler *timerHandler;


/**
 *endDate
 */
@property (nonatomic, copy) NSDate *endDate;

@end

@implementation CustomProjectCountDownView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.layer.cornerRadius = 2.0;
        self.clipsToBounds = true;
        [self setupSubviews];
        //
        self.timerHandler = [BCTimerHandler new];
        self.timerHandler.delegate = self;
    }
    return self;
}
- (void)setupSubviews {
    self.backgroundView = [UIView new];
    self.backgroundView.backgroundColor = [[UIColor darkMainColor] colorWithAlphaComponent:0.1];
    [self addSubview:self.backgroundView];
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    //
    CGSize iconSize = [self.class iconSize];
    self.leftIconIV = [UIImageView new];
    self.leftIconIV.image = LOAD_IMAGE(@"icon_countdown_h");
    [self addSubview:self.leftIconIV];
    [self.leftIconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(iconSize);
    }];
    self.leftLabel = [UILabel new];
    self.leftLabel.textColor = [UIColor darkMainColor];
    self.leftLabel.font = [self.class textFont];
    self.leftLabel.numberOfLines = 2;
    [self addSubview:self.leftLabel];
    [self.leftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.leftIconIV.mas_right).offset(5.0);
        make.right.mas_equalTo(self.mas_centerX).offset(-5.0);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self);
    }];
    
    
    self.rightIconIV = [UIImageView new];
    self.rightIconIV.image = LOAD_IMAGE(@"icon_time_n");
    [self addSubview:self.rightIconIV];
    [self.rightIconIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_centerX);
        make.centerY.mas_equalTo(self);
        make.size.mas_equalTo(iconSize);
    }];
    self.rightLabel = [UILabel new];
    self.rightLabel.textColor = [UIColor qmui_colorWithHexString:@"#9B9B9B"];
    self.rightLabel.font = [self.class textFont];
    self.rightLabel.numberOfLines = 2;
    [self addSubview:self.rightLabel];
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.rightIconIV.mas_right).offset(5.0);
        make.right.mas_equalTo(self).offset(-2.0);
        make.centerY.mas_equalTo(self);
        make.height.mas_equalTo(self);
    }];
}

- (void)setTimeConsumingString:(NSString *)timeConsumingString {
    _timeConsumingString = timeConsumingString;
    //
    self.rightLabel.text = timeConsumingString;
}
//
- (void)setCountdownString:(NSString *)mess endTime:(NSString *)time {
    self.mess = mess;
    self.timeString = time;
    //
    self.endDate = [NSDate stringToDate:time format:FullDateFormateString];
    [self timerHandlerRepeatsAction];
    [self.timerHandler start];
}
#pragma mark - BCTimerHandlerDelegate
- (void)timerHandlerRepeatsAction {
    NSDate *date = [BCServiceTimestampManager shared].date;
    NSString *countdown = [NSDate countdownStringBetweenDate:date endDate:self.endDate];
    self.leftLabel.text = [self.mess stringByReplacingOccurrencesOfString:@"{}" withString:countdown];
}


- (void)setAlpha:(CGFloat)alpha {
    [super setAlpha:alpha];
    //
    if (alpha==0) {
        [self.timerHandler stop];
    }
}

- (void)dealloc {
    [self.timerHandler stop];
}

+ (CGSize)iconSize {
    return CGSizeMake(12.0, 12.0);
}

+ (UIFont *)textFont {
    return [UIFont systemFontOfSize:11 weight:UIFontWeightRegular];
}

+ (CGFloat)defaultHeight {
    return [self textFont].lineHeight*2;
}

@end
